#include "QtUI/ConnectionListWidget.qth"
#include "QtUI/ConnectionListWidget.moh"
#include "Storage/ConnectionConfigCollection.hpp"
#include "Storage/ConnectionConfig.hpp"
#include <QtGui/QGridLayout>
#include <QtGui/QLineEdit>
#include <QtGui/QLabel>
#include <QtGui/QListWidget>
#include <algorithm>
#include <cassert>

Q_DECLARE_METATYPE( const Storage::ConnectionConfig*)

namespace QtUI
{

ConnectionListWidget::ConnectionListWidget( const Storage::ConnectionConfigCollection& connCfgColl, bool editable, QWidget* pParent)
	: QWidget( pParent)
	, rConnCfgColl_( connCfgColl)
{
	QGridLayout* pLayout = new QGridLayout;
	this->setLayout( pLayout);

	QLabel* pSelConnLabel = new QLabel;
	pSelConnLabel->setText( tr( "Selected connection", "Selected connection label"));
	pSelConnName_ = new QLineEdit;
	pSelConnName_->setReadOnly( true);
	pSelConnLabel->setBuddy( pSelConnName_);

	pLayout->addWidget( pSelConnLabel, 0, 0);
	pLayout->addWidget( pSelConnName_, 0, 1);

	QLabel* pExistingConnLabel = new QLabel;
	pExistingConnLabel->setText( tr( "Existing connections", "Existing connections label"));
	pAvailableConnList_ = new QListWidget;
	if( !editable)
		pAvailableConnList_->setSelectionMode( QAbstractItemView::NoSelection);
	pExistingConnLabel->setBuddy( pAvailableConnList_);

	pLayout->addWidget( pExistingConnLabel, 1, 0, 1, 2);
	pLayout->addWidget( pAvailableConnList_, 2, 0, 1, 2);

	this->populateConnList();

	connect( 
		pAvailableConnList_, SIGNAL( currentTextChanged( const QString&)), 
		pSelConnName_, SLOT( setText( const QString&)));
}

// ---------------------------------------------------------------------------------------------------------------------

void ConnectionListWidget::populateConnList()
{
	Storage::ConnectionConfigCollection::ConstIterator connIter = rConnCfgColl_.begin(), 
		endIter = rConnCfgColl_.end();
	while( connIter != endIter)
	{
		QListWidgetItem* pConnItem = new QListWidgetItem;
		pConnItem->setText( QString::fromStdString( (*connIter)->name()));
		QVariant data;
		data.setValue( const_cast< const Storage::ConnectionConfig*>( *connIter));
		pConnItem->setData( Qt::UserRole, data);
		pAvailableConnList_->addItem( pConnItem);
		++connIter;
	}
}

// ---------------------------------------------------------------------------------------------------------------------

void ConnectionListWidget::selectConnCfg( const Storage::ConnectionConfig* pCfgToSelect)
{
	assert( rConnCfgColl_.end() != std::find( rConnCfgColl_.begin(), rConnCfgColl_.end(), pCfgToSelect));
	int count = pAvailableConnList_->count();
	QVariant searchedData;
	searchedData.setValue( pCfgToSelect);
	for( int index = 0; index < count; ++index)
	{
		QListWidgetItem* pCurrentItem = pAvailableConnList_->item( index);
		if( searchedData == pCurrentItem->data( Qt::UserRole))
		{
			pCurrentItem->setSelected( true);
			pSelConnName_->setText( pCurrentItem->text());
			return;
		}
	}
	// If reached this point... nothing found!!
	assert( false);
}

// ---------------------------------------------------------------------------------------------------------------------

const Storage::ConnectionConfig* ConnectionListWidget::selectedConnCfg() const
{
	QListWidgetItem* currentItem = pAvailableConnList_->item( pAvailableConnList_->currentRow());
	if( NULL == currentItem)
	{
		return NULL;
	}

	return currentItem->data( Qt::UserRole).value< const Storage::ConnectionConfig*>();
}

} // namespace QtUI
